package com.ywm.eduOrder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ywm.commonUtil.JwtUtils;
import com.ywm.commonUtil.R;
import com.ywm.eduOrder.entity.TOrder;
import com.ywm.eduOrder.service.TOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-06
 */
@RestController
@RequestMapping("/eduOrde/order")
public class TOrderController {
    @Autowired
    private TOrderService orderService;

    @GetMapping("test")
    public R test(){
        return R.ok().data("String","success");
    }

    @PostMapping("/createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request){
        //从请求头中返回用户id
        String token = JwtUtils.getMemberIdByJwtToken(request);
        //创建订单返回订单号
        String orderNO=orderService.createOrders(courseId,token);
        return R.ok().data("orderNo",orderNO);
    }
    @GetMapping("/getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable("orderId") String orderId){
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        return R.ok().data("item",orderService.getOne(wrapper));
    }
    //根据课程id和用户id查询订单中订单状态
    @GetMapping("/isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable String courseId,@PathVariable String memberId){
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);
        int count = orderService.count(wrapper);
        return count >= 1;
    }
}

